# Autosub Db.py - http://code.google.com/p/auto-sub/
#
# The Autosub checkSub module
#

import logging
import os

# Autosub specific modules
import autosub.Bierdopje
import autosub.Db

# Settings
log = logging.getLogger('thelogger')


class checkSub():
    """
    Check the Bierdopje API for subtitles of episodes that are in the WANTEDQUEUE.
    If the subtitles are found, they are added to the TODOWNLOADQUEUE
    """
    def run(self):
        log.debug("checkSub: Starting round of checkSub")
        toDelete_wantedQueue = []

        if autosub.TODOWNLOADQUEUELOCK or autosub.WANTEDQUEUELOCK:
            log.debug("checkSub: Exiting, another threat is using the queues")
            return False
        else:
            autosub.TODOWNLOADQUEUELOCK = True
            autosub.WANTEDQUEUELOCK = True
        
        dbItems = autosub.Helpers.ReadDb()
        
        for index, wantedItem in enumerate(dbItems):
            
            episodeid = wantedItem['episodeid']
            season = wantedItem['season']
            episode = wantedItem['episode']
            originalfile = wantedItem['videofile']
            srtfile = wantedItem['nlsrtfile']
            engsrtfile = wantedItem['ensrtfile']
            showid = wantedItem['showid']
            nlwanted = wantedItem['nlwanted']
            enwanted = wantedItem['enwanted']
            localshowid = wantedItem['localshowid']
            showname = autosub.Helpers.getShowName(localshowid)
            
            if nlwanted == 1:
                downloadLink = autosub.Bierdopje.getSubLink(showid, 'nl', wantedItem)
                if downloadLink:
                    log.info("checkSub: The episode %s - Season %s Episode %s has a matching subtitle on bierdopje, adding to toDownloadQueue" % (showname, season, episode))
                    nlwanted = 2
                    autosub.Helpers.addSnatched(episodeid, downloadLink, 'nl')
            
            if enwanted == 1:
                downloadLink = autosub.Bierdopje.getSubLink(showid, 'en', wantedItem)
                if downloadLink:
                    log.info("checkSub: The episode %s - Season %s Episode %s has a matching subtitle on bierdopje, adding to toDownloadQueue" % (showname, season, episode))
                    enwanted = 2
                    autosub.Helpers.addSnatched(episodeid, downloadLink, 'en')
            
            if enwanted == 4 and nlwanted == 1:
                downloadLink = autosub.Bierdopje.getSubLink(showid, 'en', wantedItem)
                if downloadLink:
                    log.info("checkSub: The episode %s - Season %s Episode %s has a matching subtitle on bierdopje, adding to toDownloadQueue" % (showname, season, episode))
                    enwanted = 2
                    autosub.Helpers.addSnatched(episodeid, downloadLink, 'en')
                        
        log.debug("checkSub: Finished round of checkSub")
        autosub.TODOWNLOADQUEUELOCK = False
        autosub.WANTEDQUEUELOCK = False
        
        autosub.DOWNLOADSUBS.runnow = True
        
        return True
